package cn.good.yan.service.t1;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 1 测试 Redis
 *
 * @author shengren.yan
 * @create 2023-11-08
 */
@Service
@Slf4j
public class OrderService {

    @Resource
    private RedisTemplate redisTemplate;
    //    private StringRedisTemplate redisTemplate;

    // 一般某个server层一个公共的key的前缀
    public static final String ORDER_KEY = "ord:";

    /**
     *  新增到redis中
     */
    public void add(){
        // 随机生成key
        int keyId = ThreadLocalRandom.current().nextInt(1000) + 1;
        // 随机生成value
        String serialNo = UUID.randomUUID().toString();
        String key = ORDER_KEY + keyId;
        String value = "Order订单" + serialNo;
        redisTemplate.opsForValue().set(key, value);
        log.info("============key:{}", key);
        log.info("============value:{}", value);
    }

    /**
     * 获取key
     *
     * @param keyId
     * @return {@link String}
     */
    public String getKey(Integer keyId) {
        return (String) redisTemplate.opsForValue().get(ORDER_KEY + keyId);
    }

}
